package com.tzinfo.tzb;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tzinfo.data.JudgeStatusResult;
import com.tzinfo.data.TztData;
import com.tzinfo.data.UserInfo;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.util.HttpUtils;
import com.tzinfo.tzt.util.ResultMessage_NoData;
import com.yxw.view.util.ResFinder;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

public class TzbMainActivity extends TzbBaseActivity {

	private TabHost tabHost;
	public static TzbMainActivity activity;
	private LocalActivityManager mlam;
	public static int tabNo;

	private static final Class[] activityClass = { TzbTabBangActivity.class,
			TzbTabDongtaiActivity.class, TzbTabFaxianActivity.class,
			TzbTabTuijianActivity.class, TzbTabMyActivity.class };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tzb_ac_main);
		activity = this;
		mlam = new LocalActivityManager(this, false);
		mlam.dispatchCreate(savedInstanceState);
		init();
	}

	private void init() {
		tabHost = (TabHost) findViewById(android.R.id.tabhost);
		// 不集成TabActivity时需要这样加载tabHost
		tabHost.setup(mlam);
		createTabs();

		Intent intent = getIntent();
		tabNo = intent.getIntExtra("TabNo", 0);
		tabHost.setCurrentTab(tabNo);
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				if ("1".equals(tabId)){
					if (TzbTabDongtaiActivity.activity != null
							&& TzbTabDongtaiActivity.activity.isReload) {
						TzbTabDongtaiActivity.activity.initData();
					}
				}else if ("2".equals(tabId)){
					if (TzbTabFaxianActivity.activity != null
							&& TzbTabFaxianActivity.activity.isReload) {
						TzbTabFaxianActivity.activity.initData();
					}
				}else if ("4".equals(tabId)) {
					if (TzbTabMyActivity.activity != null
							&& TzbTabMyActivity.activity.isReload) {
						TzbTabMyActivity.activity.initData();
					}
				}
			}
		});
	}

	private void createTabs() {
		for (int i = 0; i < 5; i++) {
			TabHost.TabSpec localTabSpec1 = this.tabHost.newTabSpec("" + i);
			View localView = getLayoutInflater().inflate(
					ResFinder.getR(this,
							"R.layout.tzb_tabhost_bottom_indicator"), null);
			// 标题
			TextView localTextView = (TextView) localView
					.findViewById(ResFinder.getR(localView.getContext(),
							"R.id.title"));
			localTextView.setText(ResFinder.getResString(this,
					"R.string.tzb_bottom_tab_text" + (i + 1)));
			// 图片
			ImageView localImageView = (ImageView) ResFinder.findViewById(localView, "R.id.icon");
					
			localImageView.setImageResource(ResFinder.getR(this,
					"R.drawable.tzb_bottom_tab_img_selector" + (i + 1)));
			localTabSpec1.setIndicator(localView);
			Intent localIntent = new Intent(this, activityClass[i]);
			localIntent.putExtra("tabNo", i);
			localTabSpec1.setContent(localIntent);
			this.tabHost.addTab(localTabSpec1);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (TzbTabMyActivity.activity != null) {
			if (TzbTabMyActivity.isNeedReflash) {
				TzbTabMyActivity.activity.onReFlash();
			}
			if(TzbTabMyActivity.reflashInfo){
				TzbTabMyActivity.activity.onReFlashInfo();
			}
			if(TzbTabMyActivity.isNeedReLoadNum){
				TzbTabMyActivity.activity.onReFlashNum();
			}
		}
	}

}
